/**
 * @author : Arthur
 * @since : 2018-9-13 14:36
 */
var demandExplain;
var technologyExplain;
var href;

$(function () {
    demandExplain = UM.getEditor('demandExplain', {
        textarea: 'demandExplain',
        initialFrameHeight: 220,
        initialContent: ''
    });
    technologyExplain = UM.getEditor('technologyExplain', {
        textarea: 'technologyExplain',
        initialFrameHeight: 220,
        initialContent: ''
    });
    loadCombTree();
    createFileInput(0);
    createFileInput(1);
});

function createFileInput(num) {
    var markNum = 0;
    if (0 === num) {
        markNum = $('#demandExplainFile_div table tbody tr').length;
        $('#demandExplainFile_div table tbody').append(
            '<tr>' +
            ' <td>' +
            '  <input type="file" name="demandExplainFile[' + markNum + ']" style="width: 100%;"/>' +
            ' </td>' +
            ' <td onclick="this.children[0].click()">' +
            '  <i class="fa fa-minus" aria-hidden="true" style="margin: 5px 10px" onclick="deleteThisLine(this)"></i>' +
            ' </td>' +
            '</tr>'
        );
    } else {
        markNum = $('#technologyExplainFile_div table tbody tr').length;
        $('#technologyExplainFile_div table tbody').append(
            '<tr>' +
            ' <td>' +
            '  <input type="file" name="technologyExplainFile[' + markNum + ']" style="width: 100%;"/>' +
            ' </td>' +
            ' <td onclick="this.children[0].click()">' +
            '  <i class="fa fa-minus" aria-hidden="true" style="margin: 5px 10px" onclick="deleteThisLine(this)"></i>' +
            ' </td>' +
            '</tr>'
        );
    }
}

function deleteThisLine(dom) {
    $(dom).parent().parent().remove();
}

function ssYyChange(value) {
    $.ajax({
        url: "../../manage/apply/ssYyChange.do",
        data: {
            reqData: {
                ssYy: value ? value : $('#ssYy').val()
            }
        },
        type: "post",
        timeout: sys_timeout,
        dataType: "json"
    }).fail(function (XMLHttpRequest, textStatus) {
        innerToast.error(textStatus);
    }).done(function (responseText) {
        if (checkResponse(responseText)) {
            $('#funcHref').val(responseText.repData.href);
        } else {
            innerToast.warn(responseText.msg);
        }
    });
}

function loadCombTree() {
    $('#sjGn').combotree({
        url: '../../manage/apply/getSjFunc.do',
        editable: false,
        multiple: false,
        required: true,
        lines: true,
        animate: true
    });
    $('#ssYy').combobox({
        onSelect : function (record) {
            ssYyChange(record.value);
        }
    });
}

function subForm() {
    if (isNull($('#sjGn').val())) {
        innerToast.info('请选择上级功能');
        $('#sjGn').focus();
        return;
    }
    if (isNull($('#gnMc').val())) {
        innerToast.info('请填写功能名称');
        $('#gnMc').focus();
        return;
    }
    if (isNull($('#funcExplain').val())) {
        innerToast.info('请填写功能描述');
        $('#funcExplain').focus();
        return;
    }
    if(!demandExplain.hasContents()){
        innerToast.info('请填写需求说明');
        demandExplain.focus();
        return;
    }
    if(!technologyExplain.hasContents()){
        innerToast.info('请填写技术说明');
        technologyExplain.focus();
        return;
    }
    flavr.confirm("确定要提交吗", startSubmit);
}

function startSubmit() {
    $.messager.progress({
        title: '提示',
        msg: '保存中，请稍候……',
        text: ''
    });

    $('#demandExplain').text(Base64.encode(demandExplain.getContent()));
    $('#technologyExplain').text(Base64.encode(technologyExplain.getContent()));
    demandExplain.sync();
    technologyExplain.sync();

    $('#applyForm').form('submit', {
        url: '../../manage/apply/establishApply.do',
        error: function (XMLHttpRequest, textStatus) {
            innerToast.error(textStatus);
        },
        success: function (responseText) {
            responseText = $.parseJSON(responseText);
            $.messager.progress('close');
            if (checkResponse(responseText)) {
                innerToast.success(responseText.msg);
            } else {
                innerToast.warn(responseText.msg);
            }
        }
    });

    setTimeout(function () {
        $('#demandExplain').html(Base64.decode($('#demandExplain').text()));
        $('#technologyExplain').html(Base64.decode($('#technologyExplain').text()));
    }, 100);
}

